#include "selfinfowidget.h"
#include "model/datacenter.h"

SelfInfoWidget::SelfInfoWidget(QWidget *parent)
    :QDialog(parent)
{
    // 设置窗口属性
    this->setFixedSize(450, 200);
    this->setWindowTitle("个人信息");
    this->setWindowIcon(QIcon(":/resource/image/logo.png"));
    // 窗口关闭时销毁这个对象
    this->setAttribute(Qt::WA_DeleteOnClose);
    // 把窗口移动到鼠标当前位置
    this->move(QCursor::pos());

    // 创建网格布局管理器
    _layout = new QGridLayout();
    _layout->setHorizontalSpacing(5);
    _layout->setVerticalSpacing(3);
    _layout->setContentsMargins(5, 5, 5, 0);
    _layout->setAlignment(Qt::AlignTop);
    this->setLayout(_layout);

    // 创建头像
    _avatarBtn = new QPushButton();
    _avatarBtn->setFixedSize(75, 75);
    _avatarBtn->setIconSize(QSize(75, 75));
    _avatarBtn->setStyleSheet("QPushButton { border: none; background-color: transparent; }");
    _layout->addWidget(_avatarBtn, 0, 0, 3, 1);

    QString editsytle = "QLineEdit { border: none; border-radius: 5px; padding-left: 2px; }";
    QString labelStyle = "QLabel { font-size: 14px; font-weight: 600; }";
    QString btnStyle = "QPushButton { border: none; background-color: transparent; }";
    btnStyle += "QPushButton:pressed { background-color: rgb(210, 210, 210); }";

    int height = 30;
    // 创建id
    _idTag = new QLabel();
    _idTag->setFixedSize(50, height);
    _idTag->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
    _idTag->setText("序号");
    _idTag->setStyleSheet(labelStyle);

    _idLabel = new QLabel();
    _idLabel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);

    _layout->addWidget(_idTag, 0, 1);
    _layout->addWidget(_idLabel, 0, 2);

    // 创建昵称
    _nameTag = new QLabel();
    _nameTag->setFixedSize(50, height);
    _nameTag->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
    _nameTag->setText("昵称");
    _nameTag->setStyleSheet(labelStyle);

    _nameLabel = new QLabel();
    _nameLabel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);

    _nameModifyBtn = new QPushButton();
    _nameModifyBtn->setFixedSize(70, 25);
    _nameModifyBtn->setIconSize(QSize(25, 25));
    _nameModifyBtn->setIcon(QIcon(":/resource/image/modify.png"));
    _nameModifyBtn->setStyleSheet(btnStyle);

    _nameEdit = new QLineEdit();
    _nameEdit->setFixedHeight(height);
    _nameEdit->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
    _nameEdit->setStyleSheet(editsytle);
    _nameEdit->hide();

    _nameSubmitBtn = new QPushButton();
    _nameSubmitBtn->setFixedSize(70, 25);
    _nameSubmitBtn->setIconSize(QSize(25, 25));
    _nameSubmitBtn->setIcon(QIcon(":/resource/image/submit.png"));
    _nameSubmitBtn->setStyleSheet(btnStyle);
    _nameSubmitBtn->hide();

    _layout->addWidget(_nameTag, 1, 1);
    _layout->addWidget(_nameLabel, 1, 2);
    _layout->addWidget(_nameModifyBtn, 1, 3);

    // 创建签名
    _descTag = new QLabel();
    _descTag->setFixedSize(50, height);
    _descTag->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
    _descTag->setText("签名");
    _descTag->setStyleSheet(labelStyle);

    _descLabel = new QLabel();
    _descLabel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);

    _descModifyBtn = new QPushButton();
    _descModifyBtn->setFixedSize(70, 25);
    _descModifyBtn->setIconSize(QSize(25, 25));
    _descModifyBtn->setIcon(QIcon(":/resource/image/modify.png"));
    _descModifyBtn->setStyleSheet(btnStyle);

    _descEdit = new QLineEdit();
    _descEdit->setFixedHeight(height);
    _descEdit->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
    _descEdit->setStyleSheet(editsytle);
    _descEdit->hide();

    _descSubmitBtn = new QPushButton();
    _descSubmitBtn->setFixedSize(70, 25);
    _descSubmitBtn->setIconSize(QSize(25, 25));
    _descSubmitBtn->setIcon(QIcon(":/resource/image/submit.png"));
    _descSubmitBtn->setStyleSheet(btnStyle);
    _descSubmitBtn->hide();

    _layout->addWidget(_descTag, 2, 1);
    _layout->addWidget(_descLabel, 2, 2);
    _layout->addWidget(_descModifyBtn, 2, 3);

    // 创建邮箱
    _mailTag = new QLabel();
    _mailTag->setFixedSize(50, height);
    _mailTag->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
    _mailTag->setText("邮箱");
    _mailTag->setStyleSheet(labelStyle);

    _mailLabel = new QLabel();
    _mailLabel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);

    _mailModifyBtn = new QPushButton();
    _mailModifyBtn->setFixedSize(70, 25);
    _mailModifyBtn->setIconSize(QSize(25, 25));
    _mailModifyBtn->setIcon(QIcon(":/resource/image/modify.png"));
    _mailModifyBtn->setStyleSheet(btnStyle);

    _mailEdit = new QLineEdit();
    _mailEdit->setFixedHeight(height);
    _mailEdit->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
    _mailEdit->setStyleSheet(editsytle);
    _mailEdit->hide();

    _mailSubmitBtn = new QPushButton();
    _mailSubmitBtn->setFixedSize(70, 25);
    _mailSubmitBtn->setIconSize(QSize(25, 25));
    _mailSubmitBtn->setIcon(QIcon(":/resource/image/submit.png"));
    _mailSubmitBtn->setStyleSheet(btnStyle);
    _mailSubmitBtn->hide();

    _layout->addWidget(_mailTag, 3, 1);
    _layout->addWidget(_mailLabel, 3, 2);
    _layout->addWidget(_mailModifyBtn, 3, 3);

    // 创建验证码
    _codeTag = new QLabel();
    _codeTag->setFixedSize(50, height);
    _codeTag->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
    _codeTag->setText("验证码");
    _codeTag->setStyleSheet(labelStyle);
    _codeTag->hide();

    _codeEdit = new QLineEdit();
    _codeEdit->setFixedHeight(height);
    _codeEdit->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
    _codeEdit->setStyleSheet(editsytle);
    _codeEdit->hide();

    _getCodeBtn = new QPushButton();
    _getCodeBtn->setText("获取验证码");
    _getCodeBtn->setStyleSheet(btnStyle);
    _getCodeBtn->setFixedSize(70, 25);
    _getCodeBtn->hide();

#if TEST_UI
    _avatarBtn->setIcon(QIcon(":/resource/image/defaultAvatar.png"));
    _idLabel->setText("111");
    _nameLabel->setText("张三");
    _descLabel->setText("略略略");
    _mailLabel->setText("12321232");
#endif

    initSignalSlot();

    model::DataCenter *dataCenter = model::DataCenter::getInstance();
    model::UserInfo *myself = dataCenter->getMyself();
    if (myself != nullptr)
    {
        DEBUG() << "加载个人信息";
        _idLabel->setText(myself->_user_id);
        _nameLabel->setText(myself->_nickname);
        _descLabel->setText(myself->_desciption);
        _mailLabel->setText(myself->_mail);
        _avatarBtn->setIcon(myself->_avatar);
    }
}

void SelfInfoWidget::initSignalSlot()
{
    // 连接信号槽
    // 修改昵称按钮
    connect(_nameModifyBtn, &QPushButton::clicked, this, [=](){
        _nameLabel->hide();
        _nameModifyBtn->hide();
        _layout->removeWidget(_nameLabel);
        _layout->removeWidget(_nameModifyBtn);

        _nameEdit->show();
        _nameSubmitBtn->show();
        _nameEdit->setText(_nameLabel->text());
        _layout->addWidget(_nameEdit, 1, 2);
        _layout->addWidget(_nameSubmitBtn, 1, 3);
    });

    // 修改签名按钮
    connect(_descModifyBtn, &QPushButton::clicked, this, [=](){
        _descLabel->hide();
        _descModifyBtn->hide();
        _layout->removeWidget(_descLabel);
        _layout->removeWidget(_descModifyBtn);

        _descEdit->show();
        _descSubmitBtn->show();
        _descEdit->setText(_descLabel->text());
        _layout->addWidget(_descEdit, 2, 2);
        _layout->addWidget(_descSubmitBtn, 2, 3);
    });

    // 修改邮箱按钮
    connect(_mailModifyBtn, &QPushButton::clicked, this, [=](){
        _mailLabel->hide();
        _mailModifyBtn->hide();
        _layout->removeWidget(_mailLabel);
        _layout->removeWidget(_mailModifyBtn);

        _mailEdit->show();
        _mailSubmitBtn->show();
        _mailEdit->setText(_mailLabel->text());
        _layout->addWidget(_mailEdit, 3, 2);
        _layout->addWidget(_mailSubmitBtn, 3, 3);

        _codeTag->show();
        _codeEdit->show();
        _getCodeBtn->show();
        _layout->addWidget(_codeTag, 4, 1);
        _layout->addWidget(_codeEdit, 4, 2);
        _layout->addWidget(_getCodeBtn, 4, 3);
    });

    // 提交昵称修改处理
    connect(_nameSubmitBtn, &QPushButton::clicked, this, &SelfInfoWidget::clickNameSubmitBtn);
}

void SelfInfoWidget::clickNameSubmitBtn()
{
    const QString &newName = _nameEdit->text();
    if (newName.isEmpty())
        return;
    model::DataCenter *dataCenter = model::DataCenter::getInstance();
    connect(dataCenter, &model::DataCenter::changeNicknameDone, this, &SelfInfoWidget::clickNameSubmitBtnDone, Qt::UniqueConnection);
    dataCenter->changeNicknameAsync(newName);
}

void SelfInfoWidget::clickNameSubmitBtnDone()
{
    _layout->removeWidget(_nameEdit);
    _layout->removeWidget(_nameSubmitBtn);
    _nameEdit->hide();
    _nameSubmitBtn->hide();
    _layout->addWidget(_nameLabel, 1, 2);
    _layout->addWidget(_nameModifyBtn, 1, 3);
    _nameLabel->show();
    _nameLabel->setText(_nameEdit->text());
}
